package com.insta.esper.producerws;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.swing.JOptionPane;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * Hello world!
 * 
 */
public class App {
	private Connection connection;
	private Session session;
	private String product;
	private double price;

	private MessageProducer producer;

	public static String brokerURL;
	public final static String queueName = "passerCommande";

	private static String[] productsName = new String[] { "IPad", "IPhone",
			"IMac", "IPod", "IUnknown" };

	private static Map<String, Double[]> catalog = new HashMap<String, Double[]>();

	public void definedCalatog() {
		Double[] priceIPad = { 568.99, 499.56, 657.00 };
		catalog.put(productsName[0], priceIPad);

		Double[] priceIPhone = { 200.00, 654.00, 199.00, 0.00 };
		catalog.put(productsName[1], priceIPhone);

		Double[] priceIMac = { 1899.99, 1674.57, -1769.00 };
		catalog.put(productsName[2], priceIMac);

		Double[] priceIPod = { 156.99, -168.00 };
		catalog.put(productsName[3], priceIPod);

		Double[] priceIUnknown = { 156.99 };
		catalog.put(productsName[4], priceIUnknown);
	}

	public static void main(String[] args) throws Exception {
		App myApp = new App();
		myApp.definedCalatog();

		String str = "continue";

		while (!str.equals("non") && !str.equals("exit")) {
			String[] message = new String[2];

			message[0] = "Sélectionnez votre produit : ";
			message[1] = "Commander";

			String product = (String) JOptionPane.showInputDialog(null,
					message, message[1], JOptionPane.QUESTION_MESSAGE, null,
					productsName, "IPhone");

			if (product == null)
				System.exit(0);
			

			message[0] = "Sélectionnez votre prix : ";

			Double price = Double.parseDouble(JOptionPane.showInputDialog(null,
					message, message[1], JOptionPane.QUESTION_MESSAGE, null,
					App.catalog.get(product), "").toString());

			if (price == null)
				System.exit(0);
			
			myApp.setProduct(product);
			myApp.setPrice(price);
			myApp.exec(product, price);

			Scanner scan = new Scanner(System.in);
			System.out.println("Passer une autre commande ? ");
			str = scan.nextLine();
		}

		myApp.close();
		System.exit(0);
	}

	public App() {
	}

	public void exec(String productSelected, double priceSelected)
			throws Exception {
		brokerURL = InfoConnexionClient.getConnexionAMQ(queueName,
				productSelected, priceSelected).getUrlAMQ();

		final ConnectionFactory factory = new ActiveMQConnectionFactory(
				brokerURL);
		this.connection = factory.createConnection();
		this.connection.start();
		this.session = this.connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		this.sendMessage(productSelected + ";" + priceSelected);
	}

	public void close() throws JMSException {

		if (this.connection != null) {
			this.connection.close();
		}
	}

	public void sendMessage(String strMessage) throws JMSException {
		// setup the connection to ActiveMQ
		final Destination destination = this.session.createQueue(queueName);
		this.producer = this.session.createProducer(destination);

		// System.out.println("sendMessage() : "+strMessage);
		if (strMessage.equals("exit")) {
			System.out.println("Déconnexion ...");
		} else {
			System.out.println("[Commande envoyée depuis le WebService] : Produit : "
			 + this.product + " - " + "Prix : " + this.price);
		}

		final Message message = session.createTextMessage(strMessage);
		message.setObjectProperty("product", this.product);
		message.setObjectProperty("price", this.price);
		this.producer.send(message);
	}

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

}
